<template>
  <div class="order">
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>订单管理</el-breadcrumb-item>
      <el-breadcrumb-item>订单列表</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 卡片组件区域 -->
    <el-card>
      <!-- 搜索框区域 -->
      <el-row>
        <el-col :span="10">
          <el-input placeholder="请输入内容" v-model="queryInfo.query">
            <el-button slot="append" icon="el-icon-search"></el-button>
          </el-input>
        </el-col>
      </el-row>

      <!-- 表格区域 -->
      <el-table :data="orderList" style="width: 100%" border stripe>
        <el-table-column type="index"></el-table-column>
        <el-table-column prop="order_number" label="订单编号"></el-table-column>
        <el-table-column prop="order_price" label="订单价格" width="80"></el-table-column>
        <el-table-column prop="pay_status" label="是否付款" width="80">
          <template slot-scope="scope">
            <el-tag type="danger" v-if="scope.row.pay_status==='0'">未付款</el-tag>
            <el-tag type="success" v-else>已付款</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="is_send" label="是否发货" width="80"></el-table-column>
        <el-table-column prop="create_time" label="下单时间" width="180">
          <template slot-scope="scope">{{scope.row.create_time | dateFormat}}</template>
        </el-table-column>
        <el-table-column label="操作" width="120">
          <template slot-scope="scope">
            <el-button
              type="primary"
              icon="el-icon-edit"
              size="mini"
              @click="showEditAddressDialog"
            ></el-button>
            <el-button
              type="success"
              icon="el-icon-location"
              size="mini"
              @click="showProcessDialogVisible"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页器区域 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[2, 5, 10]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        background
      ></el-pagination>

      <!-- 编辑订单地址对话框 -->
      <el-dialog
        title="修改地址"
        :visible.sync="editAddressDialogVisible"
        width="50%"
        @close="editAddressDialogClosed"
      >
        <!-- 修改订单地址表单 -->
        <el-form
          :model="editAddressForm"
          :rules="editAddressFormRules"
          ref="editAddressFormRef"
          label-width="100px"
        >
          <el-form-item label="省市区/县" prop="address1">
            <!-- 级联选择器 -->
            <el-cascader
              v-model="editAddressForm.address1"
              :options="cityData"
              :props="{ expandTrigger: 'hover' }"
              @change="handleChange"
            ></el-cascader>
          </el-form-item>
          <el-form-item label="详细地址" prop="address2">
            <el-input v-model="editAddressForm.address2"></el-input>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="editAddressDialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="editAddressDialogVisible = false">确 定</el-button>
        </span>
      </el-dialog>

      <!-- 物流进度对话框 -->
      <el-dialog title="物流信息" :visible.sync="processDialogVisible" width="50%">
        <!-- 物流进度显示区域 -->
        <el-timeline>
          <el-timeline-item
            v-for="(activity, index) in processInfo"
            :key="index"
            :timestamp="activity.time"
          >{{activity.context}}</el-timeline-item>
        </el-timeline>
      </el-dialog>
      
    </el-card>
  </div>
</template>

<script>
// 引入城市数据（数组）
import cityData from './citydata'
export default {
  name: 'Order',
  data() {
    return {
      // 表格数据
      orderList: [],
      // 搜索框内容容器
      queryInfo: {
        query: '',
        pagenum: 1,
        pagesize: 5
      },
      // 订单总数
      total: 0,
      // 控制编辑订单显示地址对话框的显示和隐藏
      editAddressDialogVisible: false,
      // 编辑订单地址表单
      editAddressForm: {
        // 省市区县地址
        address1: [],
        // 详细地址
        address2: ''
      },
      // 编辑订单地址表单规则
      editAddressFormRules: {
        address1: [{ required: true, message: '请选择地址', trigger: 'blur' }],
        address2: [
          { required: true, message: '请输入详细地址', trigger: 'blur' }
        ]
      },
      // 城市数据
      cityData,
      // 控制物流进入的显示和隐藏
      processDialogVisible: false,
      // 物流进度数据
      processInfo: []
    }
  },
  created() {
    this.getOrderList()
  },
  methods: {
    // 获取订单列表数据
    async getOrderList() {
      const { data: res } = await this.$http.get('orders', {
        params: this.queryInfo
      })
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      // 给数据容器赋值
      this.total = res.data.total
      this.orderList = res.data.goods
      console.log(this.orderList)
    },
    // 页面大小改变时触发
    handleSizeChange(newPageSize) {
      // 改变订单请求参数
      this.queryInfo.pagesize = newPageSize
      // 重新发起请求
      this.getOrderList()
    },
    // 翻页时触发
    handleCurrentChange(newPageNum) {
      // 改变订单列表请求参数
      this.queryInfo.pagenum = newPageNum
      // 重新发起请求
      this.getOrderList()
    },
    // 显示编辑订单地址对话框
    showEditAddressDialog() {
      this.editAddressDialogVisible = true
    },
    // 级联选择器内容改变触发
    handleChange() {},
    // 关闭编辑地址对话框触发
    editAddressDialogClosed() {
      // 重置表单数据
      this.$refs.editAddressFormRef.resetFields()
    },
    // 显示物流进度对话框
    async showProcessDialogVisible() {
      // 获取物流进度数据
      const { data: res } = await this.$http.get('/kuaidi/1106975712662')
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      this.$message.success('获取物流信息成功！')
      // 给物流信息进度容器赋值
      this.processInfo = res.data
      // 显示物流进度对话框
      this.processDialogVisible = true
      console.log(res.data)
    }
  }
}
</script>

<style lang="less" scoped>
.el-cascader {
  width: 100%;
}
</style>